package com.lx.input.mutiple;

import com.lx.input.flatFile.AlipayTranDo;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.MultiResourceItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

/**
 * @author lixing
 * @date 2021/7/12 20:25
 */
@Configuration
public class MutipleFileDemoReaderConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    @Qualifier("MutipleFileItemWriterDemo")
    private ItemWriter<? super AlipayTranDo> MutipleFileItemWriterDemo;

    @Value("classpath*:/data/init/springbatchtest*.csv")
    private Resource[] resources;

    @Bean
    public Job MutipleFileDemoReaderJob3() {
        return jobBuilderFactory.get("MutipleFileDemoReaderJob3")
                .start(MutipleFileDemoItemReaderJobStep3())
                .build();

    }

    @Bean
    public Step MutipleFileDemoItemReaderJobStep3() {
        return stepBuilderFactory.get("MutipleFileDemoItemReaderJobStep3")
                .<AlipayTranDo, AlipayTranDo>chunk(100)
                .reader(MutipleResourceItemReaderDemo())
                .writer(MutipleFileItemWriterDemo)
                .build();
    }

    @Bean
    @StepScope
    public MultiResourceItemReader<AlipayTranDo> MutipleResourceItemReaderDemo() {
        MultiResourceItemReader<AlipayTranDo> reader=new MultiResourceItemReader<AlipayTranDo>();
        reader.setDelegate(FaltFileReaderDemo());
        reader.setResources(resources);
        return reader;
    }

    @Bean
    @StepScope
    public FlatFileItemReader<AlipayTranDo> FaltFileReaderDemo() {
        FlatFileItemReader<AlipayTranDo> reader=new FlatFileItemReader<AlipayTranDo>();
//		reader.setResource(new ClassPathResource("alipayTranDo"));
        reader.setLinesToSkip(5);

        DelimitedLineTokenizer tokenizer=new DelimitedLineTokenizer();
        tokenizer.setNames(new String[]
                {"tranId","channel","tranType","counterparty","goods","amount","isDebitCredit","state"}
        );
        DefaultLineMapper<AlipayTranDo> lineMapper=new DefaultLineMapper<AlipayTranDo>();
        lineMapper.setLineTokenizer(tokenizer);
        lineMapper.setFieldSetMapper(new AlipayTranDoMutipleMapper());
        lineMapper.afterPropertiesSet();
        reader.setLineMapper(lineMapper);
        return reader;
    }

}